package net.wanho.service.impl;

import net.wanho.po.Category;
import net.wanho.mapper.CategoryMapper;
import net.wanho.service.CategoryService;
import net.wanho.vo.CategoryVo;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    /**
     * 分类表分页查询
     * @param pageNum
     * @param pageSize
     * @param category
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Category category) {
        if(ObjectUtil.isNull(category)){
            category = new Category();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Category> categoryList = categoryMapper.select(category);
        return new PageInfo(categoryList);
    }

    /**
     * 根据主键查询分类表
     * @param catId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Category findById(Serializable catId) {
        return categoryMapper.selectById(catId);
    }

    /**
     * 分类表新增
     * @param category
     */
    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
    }

    /**
     * 修改分类表
     * @param category
     */
    @Override
    public void modify(Category category) {
        categoryMapper.update(category);
    }

    /**
     * 根据主键删除分类表
     * @param catId
     */
    @Override
    public void removeById(Serializable catId) {
        if(((String)catId).contains(",")){
            categoryMapper.deleteByIds(((String)catId).split(","));
        }else {
            categoryMapper.deleteById(catId);
        }
    }

    @Override
    public Object find(Integer type, Integer pageNum, Integer pageSize) {
        List<CategoryVo> categories = categoryMapper.selectType(type);
        /**
         * 不需要分页
         */
        if (pageNum == null && pageSize == null){

            List<CategoryVo> list = categories.stream()
                    .filter(item -> item.getCatPid() == 0)
                    .map(item -> {
                        findChildren(item, categories);
                        return item;
                    })
                    .collect(Collectors.toList());
            return list;
        }

        /**
         * 需要分页
         */
        PageHelper.startPage(pageNum, pageSize);
        List<CategoryVo> rootList = categoryMapper.selectType(1);
        PageInfo<CategoryVo> pageInfo = new PageInfo<>(rootList);

        List<CategoryVo> list = rootList.stream()
                .map(item -> {
                    findChildren(item, categories);
                    return item;
                })
                .collect(Collectors.toList());

        Map<String,Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("pageNum", pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());
        map.put("list", list);

        return map;
    }

    /**
     * 递归查询子节点
     * @param item
     * @param categories
     */
    private void findChildren(CategoryVo item, List<CategoryVo> categories) {
        List<CategoryVo> children = categories.stream()
                .filter(subItem -> subItem.getCatPid().equals(item.getCatId()))
                .map(subItem -> {
                    findChildren(subItem, categories);
                    return subItem;
                })
                .collect(Collectors.toList());
        item.setChildren(children);

    }
}
